package com.ptt.sqm.component;

import java.util.List;

import com.ptt.sqmonmobile.R;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NavigationItemAdapter extends ArrayAdapter<NavigationItem> {

	Context context;
	List<NavigationItem> navigationItemList;
	int layoutResID;

	public NavigationItemAdapter(Context context, int layoutResourceID, List<NavigationItem> listItems) {
		super(context, layoutResourceID, listItems);
		this.context = context;
		this.navigationItemList = listItems;
		this.layoutResID = layoutResourceID;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		NavigationItemHolder drawerHolder;
		View view = convertView;

		if (view == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			drawerHolder = new NavigationItemHolder();

			view = inflater.inflate(layoutResID, parent, false);
			drawerHolder.ItemName = (TextView) view.findViewById(R.id.drawer_itemName);
			drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);

			view.setTag(drawerHolder);

		} else {
			drawerHolder = (NavigationItemHolder) view.getTag();

		}

		NavigationItem dItem = (NavigationItem) this.navigationItemList.get(position);

		drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(dItem.getImgResID()));
		drawerHolder.ItemName.setText(dItem.getItemName());

		return view;
	}

	private static class NavigationItemHolder {
		TextView ItemName;
		ImageView icon;
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		if (observer != null) {
			super.unregisterDataSetObserver(observer);
		}
	}
}
